package cn.gzsxt.mapper.provider;

import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class ModularProvider {
	
	private static final Logger LOGGER=LogManager.getLogger(ModularProvider.class);
	
	/**
	 * 通过条件判断，返回对应的记录数
	 * @param condition
	 * @return
	 */
	public String count(Map<String, Object> condition){
		//第一步：编写公用的SQL语句
		String sql="SELECT count(*) FROM tb_basic_modular WHERE 1=1";
		//第二步：创建字符串构建对象
		StringBuilder builder=new StringBuilder(sql);
		//第三步：拼接动态SQL语句
		if (condition.get("MODULAR_ID")!=null) {
			builder.append(" AND MODULAR_ID=#{MODULAR_ID}");
		}
		if (condition.get("MODULAR_NAME")!=null) {
			builder.append(" AND MODULAR_NAME like CONCAT('%', #{MODULAR_NAME},'%')");
		}
		if (condition.get("MODULAR_SORT")!=null) {
			builder.append(" AND MODULAR_SORT=#{MODULAR_SORT}");
		}
		
		LOGGER.debug("ModularProvider.count:SQL语句："+builder.toString());
		
		//第四步：返回SQL语句
		return builder.toString();
	}
	
	public String deleteByIds(Map<String,Object[] > idsMap){
		//第一步：编写公共SQL
		String sql="DELETE FROM tb_basic_modular WHERE MODULAR_ID in ";
		//第二步：字符串构建对象
		StringBuilder builder=new StringBuilder(sql);
		//第三步：拼接SQL语句
		Object[] ids = idsMap.get("array");
		builder.append("(");
		for(int i=0;i<ids.length;i++){
			//Mybatis，数据元素可以使用#{array[下标]}获得对应的值
			builder.append("#{array["+i+"]},");
		}
		//删除一个逗号
		builder.delete(builder.length()-1, builder.length());
		builder.append(")");
		
		LOGGER.debug("ModularProvider.deleteByIds:批量删除SQL语句："+builder.toString());
		//第四步：返回SQL
		return builder.toString();
	}
	
	/**
	 * 根据条件查询并且分页
	 * @param condition
	 * @param start
	 * @param size
	 * @return
	 */
	public String findByConditionAndPage  (@Param("condition") Map<String, Object> condition, @Param("start") int start, @Param("size") int size){
		
		
		//第一步：编写公用的SQL语句
		String sql="SELECT * FROM tb_basic_modular WHERE 1=1";
		//第二步：创建字符串构建对象
		StringBuilder builder=new StringBuilder(sql);
		//第三步：拼接动态SQL语句
		if (condition.get("MODULAR_ID")!=null) {
			builder.append(" AND MODULAR_ID=#{condition.MODULAR_ID}");
		}
		if (condition.get("MODULAR_NAME")!=null) {
			builder.append(" AND MODULAR_NAME like CONCAT('%', #{condition.MODULAR_NAME},'%')");
		}
		if (condition.get("MODULAR_SORT")!=null) {
			builder.append(" AND MODULAR_SORT=#{condition.MODULAR_SORT}");
		}
		
		builder.append(" LIMIT #{start},#{size}");
		
		LOGGER.debug("ModularProvider.findByConditionAndPage:SQL语句："+builder.toString());
		
		//第四步：返回SQL语句
		return builder.toString();
		
	}
	
	/**
	 * 根据条件拼接动态的更新语句,拼接非空字段
	 * @param admin
	 * @return
	 */
	public String updateForNotnull(Map<String, Object> admin){
		//第一步：声明一个公用的SQL
		 String sql="UPDATE tb_basic_modular SET ";
		//第二步：创建一个字符串构建对象
		 StringBuilder builder=new StringBuilder(sql);
		//第三步：构建SQL语句
		 if (admin.get("MODULAR_NAME")!=null) {
			 builder.append(" MODULAR_NAME=#{MODULAR_NAME},");
		 }
		
		 if (admin.get("MODULAR_SORT")!=null) {
			 builder.append(" MODULAR_SORT=#{MODULAR_SORT},");
		 }
		 //删除最后一个逗号
		 builder.delete(builder.length()-1, builder.length());
		 //加上更新的条件
		 builder.append(" WHERE MODULAR_ID=#{MODULAR_ID}");
		 
			
		 LOGGER.debug("模块更新动态SQL语句："+builder.toString());
		 
		//第四步：返回SQL语句
		 return builder.toString();
	}

}
